import { defineStore } from 'pinia'
import { getBrowserTags } from '@/utils/chromeActions'

interface HomeState {
  links: LinkConfig[],
  filteredLink: LinkConfig[],
  selfTagList: LinkConfig[]
}

const linkSet: LinkConfig[] = [
  {
    name: '画图工具',
    child: [
      {
        name: '在线画图工具ProcessOn',
        link: 'https://www.processon.com/',
        icon: ''
      },
      {
        name: '在线画图工具draw.io',
        link: 'https://app.diagrams.net/',
        icon: ''
      },
      {
        name: '在线思维导图工具',
        link: 'http://www.mindline.cn/webapp',
        icon: ''
      },
      {
        name: 'PlantUML在线编辑器',
        link: 'http://haha98k.com/',
        icon: ''
      }
    ]
  },
  {
    name: '开发辅助工具',
    child: [
      {
        name: '在线编解码',
        child: [
          {
            icon: '',
            link: 'https://base64.supfree.net/',
            name: 'BASE64加密解密'
          },
          {
            icon: '',
            link: 'https://www.zxgj.cn/g/md5',
            name: 'MD5编码工具'
          },
          {
            icon: '',
            link: 'http://www.fly63.com/tool/cipher/',
            name: 'AES/DES加解密'
          },
          {
            icon: '',
            link: 'http://jwt.calebb.net/',
            name: 'JWT解码工具'
          },
          {
            icon: '',
            link: 'https://www.matools.com/code-convert-ascii',
            name: 'ASCII编解码工具'
          },
          {
            icon: '',
            link: 'https://www.zxgj.cn/g/unicode',
            name: 'Unicode编解码工具'
          },
          {
            icon: '',
            link: 'https://www.zxgj.cn/g/utf8',
            name: 'UTF-8编解码工具'
          },
          {
            icon: '',
            link: 'https://www.zxgj.cn/g/enstring',
            name: '字符串编解码工具'
          },
          {
            icon: '',
            link: 'http://tool.chinaz.com/tools/urlencode.aspx?jdfwkey',
            name: 'URL编解码工具'
          }
        ]
      },
      {
        name: '在线转换',
        child: [{
          icon: '',
          link: 'http://www.fly63.com/tool/ascii/',
          name: '在线ASCII码对照表'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/jinzhi',
          name: '通用进制转换工具'
        },
        {
          icon: '',
          link: 'http://www.binaryconvert.com/',
          name: '在线浮点数十进制转换'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/yansezhi',
          name: 'RGB颜色转换'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/unix',
          name: '时间戳转换工具'
        },
        {
          icon: '',
          link: 'http://www.fly63.com/tool/unitable/',
          name: '计量单位换算工具'
        },
        {
          icon: '',
          link: 'http://www.json.cn/',
          name: '在线JSON解析'
        },
        {
          icon: '',
          link: 'https://prettier.io/playground/',
          name: '在线JS代码格式化工具'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/sqlformat',
          name: 'SQL压缩/格式化工具'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/jsonxml',
          name: 'JSON和XML在线转换'
        },
        {
          icon: '',
          link: 'http://www.fly63.com/tool/jsonyaml/',
          name: 'JSON/YAML在线转换'
        },
        {
          icon: '',
          link: 'http://www.fly63.com/tool/renmingbi/',
          name: '人民币大小写转换工具'
        }]
      }, {
        name: '正则表达式',
        child: [{
          icon: '',
          link: 'https://jex.im/regulex/#!flags=&re=%5E(a%7Cb)*%3F%24',
          name: '正则表达式可视化工具'
        },
        {
          icon: '',
          link: 'https://regexr.com/',
          name: '正则表达式调试工具'
        }]
      }, {
        name: '网络工具',
        child: [{
          icon: '',
          link: 'https://www.ip138.com/',
          name: 'IP地址归属地查询'
        },
        {
          icon: '',
          link: 'https://www.ipip.net/ip.html',
          name: 'IP地址查询'
        },
        {
          icon: '',
          link: 'http://www.fly63.com/php/http/',
          name: 'HTTP在线接口测试工具'
        }]
      }, {
        name: '在线编译运行',
        child: [{
          icon: '',
          link: 'https://c.runoob.com/',
          name: '在线编译工具套装'
        },
        {
          icon: '',
          link: 'https://rextester.com/',
          name: 'C#在线编译运行'
        },
        {
          icon: '',
          link: 'https://www.onlinegdb.com/',
          name: 'C/C++在线编译调试'
        }]
      }, {
        name: '可视化/格式化工具',
        child: [{
          icon: '',
          link: 'https://codepen.io/',
          name: '在线前端编辑器codepen'
        },
        {
          icon: '',
          link: 'https://flourish.studio/',
          name: '在线数据可视化'
        },
        {
          icon: '',
          link: 'https://enjoycss.com/',
          name: '在线CSS代码可视化工具'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/xmlformat',
          name: 'XML格式化工具'
        }]
      }, {
        name: '在线生成器',
        child: [{
          icon: '',
          link: 'https://www.zxgj.cn/g/uuid',
          name: 'UUID在线生成器'
        },
        {
          icon: '',
          link: 'https://www.zxgj.cn/g/suijishu',
          name: '随机数生成器'
        }]
      }, {
        name: '其他常用工具',
        child: [{
          icon: '',
          link: 'https://www.digitalocean.com/community/tools/nginx#?',
          name: '在线Nginx配置工具'
        },
        {
          icon: '',
          link: 'http://www.fly63.com/tool/textdiff/',
          name: '在线对比工具'
        },
        {
          icon: '',
          link: 'https://www.crx4chrome.com/',
          name: '在线Chrome浏览器插件'
        },
        {
          icon: '',
          link: 'http://www.docway.net/',
          name: '在线接口文档管理工具'
        }]
      }
    ]
  },
  {
    name: '在线素材库',
    child: [{
      icon: '',
      link: 'http://pngimg.com/',
      name: '免费透明背景图片素材'
    },
    {
      icon: '',
      link: 'https://emoji.svend.cc/',
      name: 'Emoji表情搜索'
    },
    {
      icon: '',
      link: 'https://emojiisland.com/',
      name: 'Emoji表情包下载'
    },
    {
      icon: '',
      link: 'https://www.iconfinder.com/',
      name: 'ICON图标在线下载'
    },
    {
      icon: '',
      link: 'https://feathericons.com/',
      name: 'open source icons'
    },
    {
      icon: '',
      link: 'https://www.iconfont.cn/',
      name: '阿里巴巴矢量图标库'
    },
    {
      icon: '',
      link: 'https://fabiaoqing.com/',
      name: '表情包在线网站'
    },
    {
      icon: '',
      link: 'https://bz.zzzmh.cn/',
      name: '极简壁纸'
    },
    {
      icon: '',
      link: 'https://wall.alphacoders.com/',
      name: 'Wallpaper Abyss 壁纸'
    },
    {
      icon: '',
      link: 'https://pluspng.com/',
      name: '免费PNG图片库'
    },
    {
      icon: '',
      link: 'https://pixabay.com/zh/',
      name: 'Pixabay图片素材库'
    },
    {
      icon: '',
      link: 'https://unsplash.com/',
      name: 'Unsplash图片素材库'
    },
    {
      icon: '',
      link: 'http://www.pexels.com/',
      name: 'Pexels图片素材库'
    },
    {
      icon: '',
      link: 'https://images.nasa.gov/',
      name: 'NASA图片视频素材库'
    }]
  },
  {
    name: '设计制作类工具',
    child: [{
      icon: '',
      link: 'https://www.uupoop.com/#/old',
      name: '在线PS'
    },
    {
      icon: '',
      link: 'https://www.weixinsyt.com/',
      name: '在线音频剪辑'
    },
    {
      icon: '',
      link: 'https://www.kapwing.com/',
      name: '在线视频剪辑'
    },
    {
      icon: '',
      link: 'http://www.uugai.com/',
      name: '免费logo在线制作'
    },
    {
      icon: '',
      link: 'https://www.qt86.com/',
      name: '艺术字体在线生成'
    },
    {
      icon: '',
      link: 'https://tableconvert.com/',
      name: '在线表格转换工具'
    },
    {
      icon: '',
      link: 'https://www.designcap.com/',
      name: '在线海报设计工具'
    },
    {
      icon: '',
      link: 'https://bigjpg.com/',
      name: '图片智能放大工具'
    },
    {
      icon: '',
      link: 'https://mh.cli.im/',
      name: '二维码美化器'
    },
    {
      icon: '',
      link: 'https://carbon.now.sh/',
      name: '在线代码截图工具'
    },
    {
      icon: '',
      link: 'https://www.remove.bg/zh',
      name: '在线抠图工具'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/php/ico/',
      name: 'ICO图标在线生成'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/tool/svg2img/',
      name: 'SVG转PNG工具'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/tool/giftxt/',
      name: '视频转GIF工具'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/tool/ewm/',
      name: '二维码在线生成器'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/php/decoder/',
      name: '二维码在线解码'
    }]
  },
  {
    name: '写作辅助类工具',
    child: [{
      icon: '',
      link: 'https://www.eteste.com/',
      name: '在线字数统计'
    },
    {
      icon: '',
      link: 'https://mdnice.com/',
      name: 'mdnice markdown排版工具'
    },
    {
      icon: '',
      link: 'http://md.aclickall.com/',
      name: 'md2all markdown排版工具'
    },
    {
      icon: '',
      link: 'https://picx.xpoet.cn/',
      name: '图片上传 | PicX 图床神器'
    },
    {
      icon: '',
      link: 'https://sm.ms/',
      name: '在线免费图床'
    },
    {
      icon: '',
      link: 'https://imgkr.com/',
      name: '图壳图床'
    },
    {
      icon: '',
      link: 'https://urlify.cn/',
      name: '在线短链接工具'
    },
    {
      icon: '',
      link: 'http://www.fly63.com/tool/textreplace/',
      name: '在线文本替换'
    }]
  },
  {
    name: '在线办公工具',
    child: [{
      icon: '',
      link: 'https://tools.pdf24.org/zh/',
      name: 'pdf在线处理套装1'
    },
    {
      icon: '',
      link: 'https://smallpdf.com/cn/pdf-tools',
      name: 'pdf在线处理套装2'
    },
    {
      icon: '',
      link: 'https://cn.office-converter.com/',
      name: '在线多媒体转换器合集'
    },
    {
      icon: '',
      link: 'https://ocr.wdku.net/',
      name: '在线文字识别工具'
    },
    {
      icon: '',
      link: 'https://docsmall.com/',
      name: '在线文件压缩工具'
    }]
  },
  {
    name: '文档笔记',
    child: [{
      icon: '',
      link: 'https://www.yinxiang.com/',
      name: '印象笔记'
    },
    {
      icon: '',
      link: 'https://note.youdao.com/',
      name: '有道笔记'
    },
    {
      icon: '',
      link: 'https://www.onenote.com/',
      name: 'OneNote'
    },
    {
      icon: '',
      link: 'https://mubu.com/',
      name: '幕布'
    },
    {
      icon: '',
      link: 'https://www.wiz.cn/',
      name: '为知笔记'
    },
    {
      icon: '',
      link: 'https://shimo.im/',
      name: '石墨文档'
    },
    {
      icon: '',
      link: 'https://simplenote.com/',
      name: 'Simplenote'
    },
    {
      icon: '',
      link: 'https://www.yuque.com/',
      name: '语雀'
    }]
  },
  {
    name: '在线编程学习',
    child: [{
      icon: '',
      link: 'https://www.bilibili.com/',
      name: 'bilibili哔哩哔哩'
    },
    {
      icon: '',
      link: 'https://www.dotcpp.com/',
      name: 'c语言网'
    },
    {
      icon: '',
      link: 'https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5',
      name: 'cppreference'
    },
    {
      icon: '',
      link: 'https://www.icourse163.org/',
      name: '中国大学MOOC'
    },
    {
      icon: '',
      link: 'https://open.163.com/',
      name: '网易公开课'
    },
    {
      icon: '',
      link: 'https://www.nowcoder.com/',
      name: '牛客网'
    },
    {
      icon: '',
      link: 'https://codegym.cc/',
      name: 'CodeGym'
    },
    {
      icon: '',
      link: 'https://beginnersbook.com/',
      name: 'BeginnersBook'
    },
    {
      icon: '',
      link: 'http://www.javased.com/',
      name: 'JavaSED'
    },
    {
      icon: '',
      link: 'https://www.codecademy.com/',
      name: 'codecademy'
    },
    {
      icon: '',
      link: 'https://www.coursera.org/',
      name: 'Coursera'
    },
    {
      icon: '',
      link: 'https://stackoverflow.com/',
      name: 'stackoverflow'
    },
    {
      icon: '',
      link: 'https://leetcode-cn.com/',
      name: 'LeetCode'
    },
    {
      icon: '',
      link: 'https://www.lintcode.com/',
      name: 'LintCode'
    }]
  },
  {
    name: '在线教程和文档',
    child: [{
      icon: '',
      link: 'https://git-scm.com/book/zh/v2',
      name: 'Git中文教程'
    },
    {
      icon: '',
      link: 'http://svnbook.red-bean.com/nightly/zh/index.html',
      name: 'SVN中文手册'
    },
    {
      icon: '',
      link: 'https://jquery.cuishifeng.cn/',
      name: 'jQuery API中文文档'
    },
    {
      icon: '',
      link: 'https://www.nginx.cn/doc/index.html',
      name: 'Nginx中文文档'
    },
    {
      icon: '',
      link: 'https://kafka.apachecn.org/',
      name: 'Kafka中文文档'
    },
    {
      icon: '',
      link: 'https://mybatis.org/mybatis-3/zh/index.html',
      name: 'Mybatis中文文档'
    },
    {
      icon: '',
      link: 'https://developers.weixin.qq.com/miniprogram/dev/framework/',
      name: '微信小程序官方文档'
    },
    {
      icon: '',
      link: 'http://nodejs.cn/learn/',
      name: 'Nodejs中文教程文档'
    },
    {
      icon: '',
      link: 'http://httpd.apache.org/docs/',
      name: 'Apache Web Server文档'
    },
    {
      icon: '',
      link: 'https://www.springcloud.cc/spring-reference.html',
      name: 'Spring文档中文版'
    },
    {
      icon: '',
      link: 'https://studygolang.com/pkgdoc',
      name: 'Golang标准库文档中文版'
    },
    {
      icon: '',
      link: 'https://docs.oracle.com/javase/8/docs/api/index.html',
      name: 'Java8官方文档'
    },
    {
      icon: '',
      link: 'http://maven.apache.org/guides/',
      name: 'Maven官方文档'
    },
    {
      icon: '',
      link: 'http://tomcat.apache.org/tomcat-8.0-doc/index.html',
      name: 'Tomcat8官方文档'
    },
    {
      icon: '',
      link: 'https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/',
      name: 'Spring Boot官方文档'
    },
    {
      icon: '',
      link: 'https://www.rabbitmq.com/documentation.html',
      name: 'RabbitMQ官方文档'
    },
    {
      icon: '',
      link: 'http://rocketmq.apache.org/docs/quick-start/',
      name: 'RocketMQ官方文档'
    },
    {
      icon: '',
      link: 'https://dubbo.apache.org/zh/docs/',
      name: 'Dubbo中文文档'
    },
    {
      icon: '',
      link: 'https://netty.io/wiki/index.html',
      name: 'Netty官方文档'
    },
    {
      icon: '',
      link: 'https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html',
      name: 'Elasticsearch官方文档'
    },
    {
      icon: '',
      link: 'https://spring.io/projects/spring-cloud',
      name: 'Spring Cloud官方文档'
    },
    {
      icon: '',
      link: 'https://docs.docker.com/get-started/',
      name: 'Docker官方文档'
    },
    {
      icon: '',
      link: 'https://kubernetes.io/zh/docs/home/',
      name: 'kubernetes中文文档'
    },
    {
      icon: '',
      link: 'https://www.thymeleaf.org/documentation.html',
      name: 'thymeleaf官方文档'
    },
    {
      icon: '',
      link: 'https://cn.vuejs.org/v2/guide/',
      name: 'Vue.js中文文档'
    },
    {
      icon: '',
      link: 'https://reactjs.org/docs/getting-started.html',
      name: 'React.js官方文档'
    },
    {
      icon: '',
      link: 'https://www.jenkins.io/zh/doc/',
      name: 'Jenkins中文文档'
    },
    {
      icon: '',
      link: 'https://cn.vitejs.dev/',
      name: 'Home | Vite 官方中文文档'
    }]
  }
]

export const useHomeStore = defineStore({
  id: 'app',
  state: (): HomeState => ({
    links: linkSet,
    filteredLink: [],
    selfTagList: []
  }),
  getters: {
    getFilteredLink() {
      return this.filteredLink
    },
    getSelfTagLink() {
      return this.selfTagList
    },
    getFirstLevelLinks() {
      return this.links
    }
  },
  actions: {
    transferTreeNode(arr: TreeNode[]): LinkConfig[] {
      return arr.filter(key => key.url).map(item => {
        return {
          name: item.title,
          link: item.url
        }
      })
    },

    async getBrowserTagsList() {
      const tagArr = await getBrowserTags()
      const tagListArr = [] as TreeNode[]
      const flatLinks = (links: TreeNode[]) => {
        links.forEach((link: TreeNode) => {
          if (link.children && link.children.length > 0) {
            flatLinks(link.children)
          } else {
            tagListArr.push(link)
          }
        })
      }
      flatLinks(tagArr as TreeNode[])

      const transferResult = this.transferTreeNode(tagListArr)
      this.selfTagList = [...transferResult]
    },

    filterLinks(word: string) {
      if (!word) {
        this.filteredLink = []
        return
      }
      let linkArr = [] as LinkConfig[]
      const allLinks = (links: LinkConfig[]) => {
        links.forEach((link: LinkConfig) => {
          if (link.child) {
            allLinks(link.child)
          } else {
            linkArr.push(link)
          }
        })
      }

      allLinks(this.links)

      // 去重
      const obj: { [key: string]: boolean; } = {}
      linkArr = linkArr.concat(this.selfTagList).reduce((prev: LinkConfig[], current: LinkConfig) => {
        if (!obj[current.link!]) {
          prev.push(current)
          obj[current.link!] = true
        }
        return prev
      }, [])

      this.filteredLink = linkArr.filter((link) => {
        const modifyLink = link.name.toLocaleLowerCase()
        return modifyLink.includes(word.toLocaleLowerCase())
      })
    }
  }
})
